package com.salesmonitoring.accounts.view;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import javax.swing.border.Border;

import com.salesmonitoring.accounts.controller.AdminController;
import com.salesmonitoring.accounts.model.User;

@SuppressWarnings("serial")
public class EditUserUI extends JFrame implements ActionListener {

	private JPanel formPanel;
	private JLabel lblEU, lblUsername, lblPassword, lblConfirmPassword,
			lblName, lblRole, lblWarehouse, lblEmail, lblContactNo;
	private JTextField usernameTextField, contactNoTextField, nameTextField,
			emailTextField;

	private JPasswordField passwordField, confirmPasswordField;
	private JComboBox roleComboBox, warehouseComboBox;
	private JButton saveEditUserButton;
	
	private String[] names, roles, warehouses;
	private boolean roleFlag = false, passwordFlag = false, nameFlag = false, warehouseFlag = false;
	private String username;

	public EditUserUI() {
		
		AdminController adminController = AdminController.getInstance();
		this.names = adminController.getAllAONames();
		this.roles = adminController.getAllRoles();
		this.warehouses = adminController.getAllWarehouses();
		
		initialize();
	}

	private void initialize() {

		setTitle("Bank of Makati | Edit User");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 336, 313);

		formPanel = new JPanel();
		formPanel.setLayout(null);
		setContentPane(formPanel);

		lblEU = new JLabel("Edit User");
		lblEU.setHorizontalAlignment(SwingConstants.CENTER);
		lblEU.setFont(new Font("Helvetica", Font.PLAIN, 20));
		lblEU.setBounds(6, 6, 324, 32);
		formPanel.add(lblEU);

		// ---Username 
		lblUsername = new JLabel("Username");
		lblUsername.setFont(new Font("Helvetica", Font.PLAIN, 13));
		lblUsername.setBounds(6, 50, 95, 21);
		formPanel.add(lblUsername);

		usernameTextField = new JTextField();
		usernameTextField.setBounds(124, 45, 206, 28);
		usernameTextField.setText(username);
		usernameTextField.setEditable(false);
		formPanel.add(usernameTextField);
		usernameTextField.setColumns(10);

		// ---Name
		lblName = new JLabel("Name");
		lblName.setFont(new Font("Helvetica", Font.PLAIN, 13));
		lblName.setBounds(6, 80, 95, 21); //(6, 140, 95, 21); //
		formPanel.add(lblName);

		nameTextField = new JTextField();
		nameTextField.setColumns(10);
		nameTextField.setBounds(124, 75, 206, 28); //(124, 135, 206, 28);
		formPanel.add(nameTextField);

		// ---Role
		lblRole = new JLabel("Role");
		lblRole.setFont(new Font("Helvetica", Font.PLAIN, 13));
		lblRole.setBounds(6, 110, 135, 21); //(6, 170, 95, 21);
		formPanel.add(lblRole);

		roleComboBox = new JComboBox(roles);
		roleComboBox.setBounds(124, 105, 206, 28); //(124, 167, 206, 27);
		roleComboBox.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				if(roleComboBox.getSelectedItem().equals("account officer")) {
					warehouseComboBox.setEnabled(true);
					warehouseComboBox.removeAllItems();
					for(String s: warehouses) {
						warehouseComboBox.addItem(s);
					}
				} else {
					warehouseComboBox.removeAllItems();
					warehouseComboBox.setEnabled(false);
				}
				
			}
		});
		
		formPanel.add(roleComboBox);

		// ---Warehouse
		lblWarehouse = new JLabel("Warehouse");
		lblWarehouse.setFont(new Font("Helvetica", Font.PLAIN, 13));
		lblWarehouse.setBounds(6, 140, 95, 21); //(6, 200, 95, 21);
		formPanel.add(lblWarehouse);

		warehouseComboBox = new JComboBox();
		warehouseComboBox.setEnabled(false);
		warehouseComboBox.setBounds(124, 135, 206, 28); //(124, 197, 206, 27); 
		formPanel.add(warehouseComboBox);

		// ---Email
		lblEmail = new JLabel("E-mail");
		lblEmail.setFont(new Font("Helvetica", Font.PLAIN, 13)); 
		lblEmail.setBounds(6, 170, 95, 21); //(6, 230, 95, 21); 
		formPanel.add(lblEmail);

		emailTextField = new JTextField();
		emailTextField.setColumns(10);
		emailTextField.setBounds(124, 167, 206, 27); //(124, 225, 206, 28);
		formPanel.add(emailTextField);

		// ---Contact No
		lblContactNo = new JLabel("Contact No.");
		lblContactNo.setFont(new Font("Helvetica", Font.PLAIN, 13));
		lblContactNo.setBounds(6, 200, 95, 21); //(6, 260, 95, 21);
		formPanel.add(lblContactNo);

		contactNoTextField = new JTextField();
		contactNoTextField.setColumns(10);
		contactNoTextField.setBounds(124, 197, 206, 27); //(124, 255, 206, 28);
		formPanel.add(contactNoTextField);

		saveEditUserButton = new JButton("Save");
		saveEditUserButton.setBounds(210, 248, 117, 29);
		saveEditUserButton.addActionListener(this);
		formPanel.add(saveEditUserButton);

	}
	
	public void setDetails(String username, String name, String warehouse, String role) {
		usernameTextField.setText(username);
		nameTextField.setText(name);
		roleComboBox.setSelectedItem(role);
		warehouseComboBox.setSelectedItem(warehouse);
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		
		Border redBorder = BorderFactory.createEtchedBorder(Color.WHITE, Color.RED);
		Border defaultBorder = BorderFactory.createEtchedBorder(Color.WHITE, Color.GRAY);
		
		if(nameTextField.getText().equals("")) {
			nameTextField.setBorder(redBorder);
			nameTextField.requestFocus();
			nameFlag = false;
		} else {
			nameTextField.setBorder(defaultBorder);
			nameFlag = true;
		}
		
		if(roleComboBox.getSelectedItem().equals("account officer") && warehouseComboBox.getSelectedItem().equals("")) {
			warehouseComboBox.setBorder(redBorder);
			warehouseComboBox.requestFocus();
			warehouseFlag = false;
		} else {
			warehouseComboBox.setBorder(defaultBorder);
			warehouseFlag = true;
		}
		
		if(nameFlag && warehouseFlag) {
			
			User user = new User();
			
			user.setId(usernameTextField.getText());
			user.setName(nameTextField.getText());
			user.setRole(roleComboBox.getSelectedItem().toString());
			user.setWarehouse((warehouseComboBox.getSelectedItem() != null) ? warehouseComboBox.getSelectedItem().toString() : null);
			user.setEmail(emailTextField.getText());
			
			
			try {
				user.setContact((contactNoTextField.getText() != null) ? Integer.parseInt(contactNoTextField.getText()) : 0);
			} catch(Exception e) {
				JOptionPane.showMessageDialog(null, "Invalid contact number.");
				return;
			}
			
			AdminController adminController = AdminController.getInstance();
			int status = adminController.updateUser(user);
			
			if(status == 0) {
				JOptionPane.showMessageDialog(null, "Error in updating user.");
			} else {
				JOptionPane.showMessageDialog(null, "User successfully updated.");
				this.dispose();
			}
			
		} else {
			
			JOptionPane.showMessageDialog(null, "Please fill out required fields.");
			
		}
		
	
	}
}
